//
//  NearTheShopView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/4.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "NearTheShopView.h"
#import "NearTheShopViewTableViewCell.h"
#import "NearTheShopViewCollectionViewCell.h"


@interface NearTheShopView ()<UICollectionViewDataSource , UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout , UITableViewDelegate , UITableViewDataSource >

// 发布状态
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic , strong) UIView *bgView;
//@property (nonatomic , strong) UIView *swipingLineView;   /**<划动的线*/
Strong UIView *lineView;

@end

@implementation NearTheShopView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
       
    }
    return self;
}


#pragma mark - init

//- (UIView *)swipingLineView{
//    if (!_swipingLineView) {
//        _swipingLineView = [[UIView alloc]initWithFrame:CGRectMake(-0.25, 0, SCREEN_WIDTH / self.collectionDataArray.count, 1)];
//        _swipingLineView.backgroundColor = getColor(mainColor);
//    }
//    return _swipingLineView;
//}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, self.collectionView.bounds.size.height, SCREEN_WIDTH, 0.5)];
        _bgView.backgroundColor = getColor(dividerColor);
//        [_bgView addSubview:self.swipingLineView];
    }
    return _bgView;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0.5, SCREEN_WIDTH , 31 / WIDTH_5S_SCALE) collectionViewLayout:flowLayout];
        [_collectionView registerClass:[NearTheShopViewCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionView"];
        _collectionView.backgroundColor = getColor(@"ffffff");
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.collectionView.bounds.size.height + 0.5 + self.bgViewHeight, SCREEN_WIDTH,SCREEN_HEIGHT - self.collectionView.bounds.size.height - 64 - 0.5 - 32 / WIDTH_5S_SCALE - self.bgViewHeight)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        if([_tableView respondsToSelector:@selector(setLayoutMargins:)]){
            _tableView.layoutMargins=UIEdgeInsetsZero;
        }
        _tableView.backgroundColor = getColor(@"ffffff");
        _tableView.tableFooterView = [[UIView alloc]init];
        
    }
    return _tableView;
}



#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NearTheShopViewTableViewCell * cell = [NearTheShopViewTableViewCell cellForTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    return cell;
    
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80 / WIDTH_5S_SCALE;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //                SendProductViewController *vc = [SendProductViewController new];
    //            vc.hidesBottomBarWhenPushed = YES;
    //            vc.model = self.dataArray[indexPath.section];
    //            vc.type = ProductControllerType_Edit;
    //            vc.xct = self;
    //            [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NearTheShopViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionView" forIndexPath:indexPath];
    cell.model = self.collectionDataArray[indexPath.row];
    return cell;
}


#pragma mark -  UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    [UIView animateWithDuration:.5 animations:^{
//        self.swipingLineView.frame = CGRectMake(indexPath.row * (SCREEN_WIDTH / self.collectionDataArray.count), 0, SCREEN_WIDTH / self.collectionDataArray.count, 1);
//    }];
    
    [self.collectionDataArray[indexPath.row] setObject:mainColor forKey:@"textColor"];
    for (int i = 0 ; i < self.collectionDataArray.count ; i ++) {
        if (i != indexPath.row) {
            [self.collectionDataArray[i] setObject:textColor forKey:@"textColor"];
        }
    }
    if (_getCollViewCellIndexPath) {
        _getCollViewCellIndexPath((int)indexPath.row);
    }
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH / 4, 31 / WIDTH_5S_SCALE);
}

#pragma  mark - data

- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

- (void)setCollectionDataArray:(NSMutableArray *)collectionDataArray{
    _collectionDataArray = collectionDataArray;
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    _lineView.backgroundColor = getColor(dividerColor);
    [self addSubview:_lineView];
    [self addSubview:self.collectionView];
    [self addSubview:self.tableView];
    [self addSubview:self.bgView];
}


@end
